<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/07/25 14:00:21 $)
// $RCSfile: functions_syncworks.php,v $ : $Revision: 1.8 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Get the Item ID from entry
*/
function getItemIDFromEntryID($linktype, $entryid)
{
	global $dbCore;

	$_link = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."calendarlinks` WHERE `linktype` = '". intval($linktype) ."' AND entryid = '". $dbCore->escape($entryid) ."';");

	if (!empty($_link["typeid"]))
	{
		return $_link["typeid"];
	}

	return false;
}

/**
* Convert MSO Entry to SWIFT
*/
function MSOContactEntryToSWIFT($_entry)
{
	return array("fullname" => _msodata($_entry["FullName"]), "jobtitle" => _msodata($_entry["JobTitle"]), "initials" => _msodata($_entry["Initials"]), "email1address" => _msodata($_entry["Email1Address"]), "email2address" => _msodata($_entry["Email2Address"]), "email3address" => _msodata($_entry["Email3Address"]), "customerid" => _msodata($_entry["CustomerID"]), "mobiletelephonenumber" => _msodata($_entry["MobileTelephoneNumber"]), "cartelephonenumber" => _msodata($_entry["CarTelephoneNumber"]), "pagernumber" => _msodata($_entry["PagerNumber"]), "othertelephonenumber" => _msodata($_entry["OtherTelephoneNumber"]), "imaddress" => _msodata($_entry["IMAddress"]), "companyname" => _msodata($_entry["CompanyName"]), "department" => _msodata($_entry["Department"]), "officelocation" => _msodata($_entry["OfficeLocation"]), "businessaddress" => _msodata($_entry["BusinessAddress"]), "businesstelephonenumber" => _msodata($_entry["BusinessTelephoneNumber"]), "business2telephonenumber" => _msodata($_entry["Business2TelephoneNumber"]), "businessfaxnumber" => _msodata($_entry["BusinessFaxNumber"]), "businesshomepage" => _msodata($_entry["BusinessHomePage"]), "homeaddress" => _msodata($_entry["HomeAddress"]), "hometelephonenumber" => _msodata($_entry["HomeTelephoneNumber"]), "home2telephonenumber" => _msodata($_entry["Home2TelephoneNumber"]), "homefaxnumber" => _msodata($_entry["HomeFaxNumber"]), "birthdaydateline" => MSODateToDateline($_entry["Birthday"]), "anniversarydateline" => MSODateToDateline($_entry["Anniversary"]), "mailingaddress" => _msodata($_entry["MailingAddress"]), "otheraddress" => _msodata($_entry["OtherAddress"]), "webpage" => _msodata($_entry["WebPage"]), "assistantname" => _msodata($_entry["AssistantName"]), "assistanttelephonenumber" => _msodata($_entry["AssistantTelephoneNumber"]));
}

/**
* Update the Link
*/
function updateCalendarLink($linktype, $entryid, $typeid, $guidid, $lastupdate, $lastswiftupdate)
{
	global $dbCore;

	$dbCore->query("REPLACE INTO `". TABLE_PREFIX ."calendarlinks` (`linktype`, `entryid`, `typeid`, `guidid`, `lastupdate`) VALUES ('". intval($linktype) ."', '". $dbCore->escape($entryid) ."', '". intval($typeid) ."', '". intval($guidid) ."', '". intval($lastupdate) ."');", 4);

	return true;
}

/**
* Update the Link (2)
*/
function _updateCalendarLink($linktype, $entryid, $typeid, $guidid, $lastupdate, $lastswiftupdate = '')
{
	global $dbCore;

	if (empty($lastupdate) && empty($lastswiftupdate))
	{
		return false;
	}

	$dbCore->query("UPDATE `". TABLE_PREFIX ."calendarlinks` SET ". iif(!empty($lastupdate), "`lastupdate` = '". intval($lastupdate) ."'"). iif(!empty($lastswiftupdate), iif(!empty($lastupdate), ", ")."`lastswiftupdate` = '". intval($lastswiftupdate) ."'") ." WHERE `linktype` = '". intval($linktype) ."' AND `entryid` = '". $dbCore->escape($entryid) ."' AND `typeid` = '". intval($typeid) ."' AND `guidid` = '". intval($guidid) ."';", 4);

	return true;
}

/**
* Convert Dateline to MSO Date
*/
function datelineToMSODate($dateline)
{
	if (empty($dateline))
	{
		return "01/01/4501 00:00AM";
	}

	return iif($_SWIFT["settings"]["dt_caltype"]=="us", date("m/d/Y h:iA", $dateline), date("d/m/Y h:iA", $dateline));
}

/**
* Convert MSO Date to Dateline
*/
function MSODateToDateline($msodate)
{
	if ($msodate == "01/01/4501 12:00AM")
	{
		// Invalid Date
		return "0";
	}

	$month = substr($msodate, 0, 2);
	$day = substr($msodate, 3, 2);
	$year = substr($msodate, 6, 4);

	$hour = substr($msodate, 11, 2);
	$minute = substr($msodate, 14, 2);
	$ampm = substr($msodate, 16, 2);

	if (stristr($msodate, "PM"))
	{
		$euhour = $hour+12;
	} else {
		$euhour = $hour;
	}

	if ($euhour < 0)
	{
		$euhour = 0;
	}
	if ($minute < 0)
	{
		$minute = 0;
	}

	return @mktime($euhour, $minute, 0, $month, $day, $year);
}

/**
* Processes the MSO Data
*/
function _msodata($data)
{
	return iif($data=="NULL", "", $data);
}

/**
* Load the Guid ID
*/
function loadGuidID($swguid)
{
	global $dbCore, $_SWIFT;

	if (empty($swguid))
	{
		return false;
	}

	$_guidid = $dbCore->queryFetch("SELECT `guidid` FROM `". TABLE_PREFIX ."syncworksguid` WHERE `swguid` = '". $dbCore->escape($swguid) ."';");
	if (empty($_guidid["guidid"]))
	{
		// New Install
		$dbCore->query("INSERT INTO `". TABLE_PREFIX ."syncworksguid` (`swguid`, `dateline`) VALUES ('". $dbCore->escape($swguid) ."', '". DATENOW ."');");
		$guidid = $dbCore->lastInsertId();
	} else {
		$guidid = $_guidid["guidid"];
	}

	return $guidid;
}


?>